//
//  RECViewController.m
//  CarFace
//
//  Created by dlios on 15/3/23.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "RECViewController.h"
#import "ADScrollView.h"


@interface RECViewController () <UICollectionViewDelegate, UICollectionViewDataSource>
@property(nonatomic, retain)UICollectionView *colletionView;
@end

@implementation RECViewController
- (void)dealloc
{
    [_colletionView release];
    [super dealloc];
}
- (void)handleData
{
//    NSString *str = @"http://ad.app.autohome.com.cn/autov4.6/ad/FocusAdAndNewsListAd.ashx?newstype=0&pageindex=1&networkid=0&idfa=0&deviceid=352558065636350&mac=F0%3A25%3AB7%3AE2%3A30%3AE1&appid=2&platform=2&version=4.6.1&random=STulyz47U033L&a=2&pm=2&v=4.6.1%20HTTP/1.1";
    
    NSString *str = @"http://app.api.autohome.com.cn/autov4.6/news/newslist-a2-pm2-v4.6.1-c0-nt0-p1-s20-l0.html";
    // html转化为json
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSLog(@"成功%@", responseObject);
        NSError *error = nil;
        NSJSONSerialization *result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
//        NSLog(@"%@", result);
        NSMutableArray *folArr = [result valueForKey:@"result"];
        for (NSString *str in folArr) {
            str =  [folArr valueForKey:@"imgurl"];
            
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"失败%@", error);
    }];
    
    
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    
    [self handleData];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height - 113);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.colletionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    self.colletionView.delegate = self;
    self.colletionView.dataSource = self;
    [self.view addSubview:self.colletionView];
    self.colletionView.pagingEnabled = YES;
    [flowLayout release];
    [_colletionView release];
    [self.colletionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Recommand"];
    
    ADScrollView *adVC = [[ADScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.colletionView.frame.size.height / 4) ];
    [self.colletionView addSubview:adVC];
    [adVC release];
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 10;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Recommand" forIndexPath:indexPath];
    if (indexPath.row == 0) {
        cell.backgroundColor = [UIColor redColor];
    }else if (indexPath.row == 1) {
        cell.backgroundColor = [UIColor orangeColor];
    }else if (indexPath.row == 2) {
        cell.backgroundColor = [UIColor yellowColor];
    }
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
